﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OF.Utility;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleTest
{
    public class ImportCarJP
    {
        SqlConnection sqlConn;
        SqlCommand cmdStr;
        string strConn = "data source=cwtx-test01;user ID =developer;password=developer;initial catalog=CarCenter";

        public void Execute()
        {
            DirectoryInfo dir = new DirectoryInfo("E:\\work\\文档\\车牌号码");
            var files = dir.GetFiles("*.txt");
            foreach (var item in files)
            {
                string content = File.ReadAllText(item.FullName);
                CarNumber obj = Newtonsoft.Json.JsonConvert.DeserializeObject<CarNumber>(content);
                foreach (var li in obj.data.hds)
                {
                    Insert(li);
                }

            }
         
        }
              

        private void Insert(NumberRange info)
        {
            if (info!=null)
            {
                string cphmqs = info.subhd.Split('~')[0].Trim();
                string sqlStr = "insert into [newcarListBase]([cphmqj],[ceatedatetime],[cphmqs],[cphmnumber],[isstate])values('" + info.subhd + "','" + info.tfrq + "','"+ cphmqs + "','100',0)";
                sqlConn = new SqlConnection(strConn); sqlConn.Open();
                cmdStr = new SqlCommand(sqlStr, sqlConn);

                if (cmdStr.ExecuteNonQuery() > 0)
                { 
                    sqlConn.Close();
                }
            }
          
        }

        public void ExecuteBaseClear()
        {
            string sqlStr = "select * from newcarListBase where id > 1699";
            sqlConn = new SqlConnection(strConn); sqlConn.Open();
            cmdStr = new SqlCommand(sqlStr, sqlConn);
            SqlDataReader reader = cmdStr.ExecuteReader();
            while (reader.Read())
            {
                InsertBaseClear(reader["cphmqs"].ToString(), (DateTime)reader["ceatedatetime"]);
            }
        }

        private void InsertBaseClear(string cphmqs, DateTime sj)
        {
            for (int i = 0; i < 100; i++)
            {              
                string endhm = i < 10 ? "0" + i : i.ToString();
                string hm = string.Format("京{0}{1}", cphmqs.Substring(0, 4), endhm);
                string sqlStr = "insert into [newcarListBase_Clear]([hphm],[shangpaishijian],[Isstate],[cphmqs])values('" + hm + "','" + sj + "',1,'" + cphmqs + "')";
                sqlConn = new SqlConnection(strConn); sqlConn.Open();
                cmdStr = new SqlCommand(sqlStr, sqlConn);

                if (cmdStr.ExecuteNonQuery() > 0)
                {
                    sqlConn.Close();
                }
            }
           
        }
    }


    public class CarNumber
    {
        public string message { get; set; }

        public CarData data { get; set;}
    }

    public class CarData
    {
        public int totalCount { get; set; }

        public List<NumberRange> hds { get; set; }
    }

    public class NumberRange
    {
        public string bmmc { get; set; }

        public string glbm { get; set; }
        public string gxsj { get; set; }
        public string hd { get; set; }
        public string hpzl { get; set; }
        public string hpzlStr { get; set; }
        public string jyw { get; set; }
        public string qyrq { get; set; }
        public string sjsj { get; set; }
        public string subhd { get; set; }
        public string tfbj { get; set; }
        public string tfrq { get; set; }
        public string xzqh { get; set; }
        public string zbsj { get; set; }
        public string zt { get; set; }
     
    }
}
